import Mock from 'mockjs'
const loginArr = [
    {username:'admin',token:'admin---token---',pwd:"123456",job:'开发',name:'小明',rule:'管理员',address:'山西'},
    {username:'user',token:'user---token---',pwd:"123456",job:'开发',name:'小红',rule:'用户',address:'北京'},
]
export default{
    getloginData:config=>{
        // console.log(config.body);
        var {username,pwd} = JSON.parse(config.body)
        var token = ''
        loginArr.forEach(item=>{
            if(item.username==username&&item.pwd==pwd){
                token = item.username + '---token---'
                item.token = token
            }
        })
        if(token){
            return {
                code:200,
                msg:'登录成功',
                data:token
            }
        }else{
            return{
                code:201,
                msg:'账号密码不正确',
                data:null
            }
        }
    },

    getUserData:config=>{
        var {token} = JSON.parse(config.body)
        var obj =  loginArr.find(item=>{
            return item.token == token
        })
        return{
            code:200,
            msg:'获取用户信息成功',
            data:obj
        }
    },

    registerData:config=>{
        const {username,pwd} = JSON.parse(config.body)
        console.log(username,pwd);
        var res = 0
        loginArr.forEach(item=>{
            if(item.username == username){
                res = 1
            }
        })
        if(res==1){
            return{
                code:201,
                msg:'您输入的昵称重复',
                data:null
            }
            
        }else{
            loginArr.push({
                username:username,
                pwd:pwd,
                job:'开发',
                name:Mock.mock('@cname()'),
                rule:Mock.mock('@integer(0, 1)')==0?'用户':'管理员',
                address:Mock.mock('@province()')
            })
            return{
                code:200,
                msg:'注册成功',
                data:loginArr
            }
        }
    }
}
